package com.github.umgr.service.impl;

import com.github.CommonConstant;
import com.github.mvc.basic.service.impl.BaseServiceImpl;
import com.github.session.LocalSession;
import com.github.session.SObject;
import com.github.umgr.mapper.UserMapper;
import com.github.umgr.pojo.entity.UserEntity;
import com.github.umgr.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Service
public class UserServiceImpl extends BaseServiceImpl<UserEntity> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean doLogin(String username, String password, HttpServletRequest request) {

        UserEntity user = userMapper.getUser(username, password);

        if (null == user)
            return false;

        if (null != user) {
            SObject session = new SObject();
            session.setUid(user.getId());
            session.setUname(user.getUserName());
            session.setOrgid(user.getCreateOrgId());
            session.setLogtime(new Date());
            LocalSession.set(session);
            request.getSession().setAttribute(CommonConstant.USER_LOGIN_SESSION, session);
            return true;
        }
        return false;
    }

}
